(function(angular) {
  'user strict';
  angular
    .module('fs.filter', [])
    .filter('encode', [
      '$filter',
      'Constants',
      function($filter, Constants) {
        return function(value) {
          if (!value || angular.isNumber(value) || !angular.isString(value)) {
            return value;
          } else if ('*' == value) {
            return $filter('all')(value);
          }
          if (value.indexOf(Constants.ENCODE_SEPARATOR) != -1) {
            value = value.substring(0, value.indexOf(Constants.ENCODE_SEPARATOR));
          }
          return value;
        };
      }
    ])
    .filter('trustedUrl', [
      '$sce',
      function($sce) {
        return function(value) {
          if (!value) {
            return value;
          }
          return $sce.trustAsUrl(value);
        };
      }
    ])
    .filter('trustedResourceUrl', [
      '$sce',
      function($sce) {
        return function(value) {
          if (!value) {
            return value;
          }
          return $sce.trustAsResourceUrl(value);
        };
      }
    ])
    .filter('trustedHtml', [
      '$sce',
      function($sce) {
        return function(value) {
          if (!value) {
            return value;
          }
          return $sce.trustAsHtml(value);
        };
      }
    ])
    .filter('numberFormat', [
      '$filter',
      '$locale',
      'formatService',
      function($filter, $locale, formatService) {
        return function(number, pattern) {
          if (isNaN(number)) {
            return number;
          }
          return formatService.formatNumber(number, pattern);
        };
      }
    ])
    .filter('moneyFormat', [
      '$filter',
      '$locale',
      'formatService',
      function($filter, $locale, formatService) {
        return function(money, pattern) {
          if (isNaN(money)) {
            return money;
          }
          return formatService.formatMoney(money, pattern);
        };
      }
    ])
    .filter('moneyFormatWithCcy', [
      '$filter',
      '$locale',
      'formatService',
      function($filter, $locale, formatService) {
        return function(money, ccy) {
          if (isNaN(money)) {
            return money;
          }
          money = formatService.formatMoney(money, ccy);
          return '<div class="report-amount"><h5>' + ccy + '</h5><h5>' + money + '</h5></div>';
        };
      }
    ])
    .filter('statusCodeFilter', [
      '$filter',
      '$locale',
      'formatService',
      function($filter, $locale, formatService) {
        return function(statusCode) {
          statusCode = angular.lowercase(statusCode);
          return '<div style="text-align: center;"><span class="icon icon-status-' + statusCode + '"></span></div>';
        };
      }
    ])
    .filter('percentFormat', [
      '$filter',
      '$locale',
      'formatService',
      function($filter, $locale, formatService) {
        return function(number, pattern) {
          return formatService.formatPercentage(number, pattern);
        };
      }
    ])
    .filter('dateFormat', [
      '$filter',
      '$locale',
      'formatService',
      function($filter, $locale, formatService) {
        return function(date, pattern) {
          if (angular.isString(date)) {
            date = formatService.parseDate(date, 'longTimestamp');
          }
          return $filter('date')(date, pattern);
        };
      }
    ])
    .filter('removeSiteCode', [
      '$filter',
      '$locale',
      'formatService',
      function($filter, $locale, formatService) {
        return function(data, pattern) {
          //in the list page if db product Code like 'FSCSBK:PROD_CODE1', the Product code in the list only show 'PROD_CODE1'. the siteCode length is 6.
          if (data) return data.substring(7, data.length);
        };
      }
    ])
    .filter('i18n', [
      '$filter',
      '$locale',
      '$translate',
      function($filter, $locale, $translate) {
        return function(resKey, pattern) {
          return $filter('translate')(resKey);
        };
      }
    ])
    .filter('yesNo', [
      '$filter',
      '$locale',
      'SYS_PARAMS',
      '$translate',
      function($filter, $locale, SYS_PARAMS, $translate) {
        return function(flag, pattern) {
          var label = flag ? SYS_PARAMS.YES_NO[flag.toUpperCase()] || flag : flag;
          return $filter('translate')(label);
        };
      }
    ])
    .filter('all', [
      '$filter',
      '$locale',
      'SYS_PARAMS',
      '$translate',
      function($filter, $locale, SYS_PARAMS, $translate) {
        return function(flag, pattern) {
          var label = flag ? SYS_PARAMS.ALL[flag.toUpperCase()] || flag : flag;
          return $filter('translate')(label);
        };
      }
    ])
    .filter('json', [
      '$filter',
      function($filter) {
        return function(value) {
          if (value) {
            var result = '';
            var obj = angular.fromJson(value);
            var label = $filter('translate')(obj['label']);
            for (var key in obj) {
              if (key != 'label') {
                result += $filter('translate')(label + key.toLowerCase()) + '=' + obj[key] + ';';
              }
            }
            return result.substring(0, result.length - 1);
          }
          return value;
        };
      }
    ])
    .filter('sensitive', function() {
      return function(value, positiveOffset, negativeOffset, mask) {
        if (value == undefined) {
          value = '';
        }
        return encryption(value, positiveOffset, negativeOffset, mask, '*');
      };
      function encryption(value, positiveOffset, negativeOffset, mask, symbol) {
        var result;
        function getAsterisk(a1) {
          var asterisk = '';
          for (var i = 0; i < a1; i++) {
            asterisk += symbol;
          }
          return asterisk;
        }
        if (positiveOffset < 0) {
          positiveOffset = 0;
        }
        if (negativeOffset < 0) {
          negativeOffset = 0;
        }
        if (mask) {
          if (positiveOffset + negativeOffset < value.length) {
            var headStr = value.substring(0, positiveOffset);
            var endStr = value.substring(value.length - negativeOffset);
            result = headStr + getAsterisk(value.length - positiveOffset - negativeOffset) + endStr;
          } else {
            result = value;
          }
          return result;
        } else {
          if (positiveOffset + negativeOffset < value.length) {
            var str = value.substring(positiveOffset, value.length - negativeOffset);
            result = getAsterisk(positiveOffset) + str + getAsterisk(negativeOffset);
          } else {
            result = getAsterisk(value.length);
          }
          return result;
        }
      }
    })
    .filter('zeroEmpty', function() {
      return function(value) {
        if (value === 0) {
          value = '';
        }
        return value;
      };
    })
    .filter('percentageFilter', function() {
      return function(value) {
        return value + '%';
      };
    })
    .filter('options', [
      '$filter',
      '$locale',
      '$translate',
      function($filter, $locale, $translate) {
        return function(optionValue, options) {
          var optionLabel = '';
          if (options && angular.isArray(options)) {
            for (var i = 0; i < options.length; i++) {
              if ($filter('encode')(optionValue) == $filter('encode')(options[i].value)) {
                optionLabel = $filter('translate')(options[i].display);
                break;
              }
            }
          }
          return optionLabel;
        };
      }
    ]);
})(angular);
